package beans;

import dao.DocumentDAO;
import model.Document;
import org.apache.commons.codec.digest.DigestUtils;
import org.primefaces.event.FileUploadEvent;
import utils.UpdateUtil;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

@ManagedBean(name = "uploadFileBean")
@RequestScoped
public class UploadFileBean {
    private static final int BUFFER_SIZE = 6124;
    private Document document;

    @ManagedProperty("#{userBean}")
    private UserBean userBean;

    public void setUserBean(UserBean userBean) {
        this.userBean = userBean;
    }

    public UploadFileBean() {
    }

    @PostConstruct
    public void init() {
        document = new Document();
    }

    public Document getDocument() {
        return document;
    }

    public void setDocument(Document document) {
        this.document = document;
    }


    public void handleFileUpload(FileUploadEvent event) {
        try {

            document.setDocumentName(event.getFile().getFileName());
            document.setDate(new Date());
            document.setSize(event.getFile().getSize() / 1024);
            document.setCode(DigestUtils.md2Hex(event.getFile().getFileName()));
            DocumentDAO documentDAO = new DocumentDAO();
            if(userBean.getUser().getRole().getIdRole() == 1) {
                document.setStorageLife(3);
            }
            if (userBean.getUser().getRole().getIdRole() != 1) {
                document.setUser(userBean.getUser());
            }
            documentDAO.insertDocument(document);

            File result = new File("D:\\FILES\\" + document.getIdDocument());
            FileOutputStream fileOutputStream = new FileOutputStream(result);
            byte[] buffer = new byte[BUFFER_SIZE];
            int bulk;
            InputStream inputStream = event.getFile().getInputstream();
            while (true) {
                bulk = inputStream.read(buffer);
                if (bulk < 0) {
                    break;
                }
                fileOutputStream.write(buffer, 0, bulk);
                fileOutputStream.flush();
            }

            fileOutputStream.close();
            inputStream.close();

            UpdateUtil.updatePage();
            String link = "http:localhost:8080/download.html?id=" + document.getIdDocument() + "&code=" + document.getCode();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Success",
                    "File: " + event.getFile().getFileName() + " was uploaded. Your link to download file: " + link));
        } catch (IOException e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "ERROR", e.toString()));
        }
    }
}
